-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] Improve the "with default" option in the has one relation. #16382
[5.3] Improve the "with default" option in the has one relation. #16382
Conversation
return call_user_func($this->withDefault); | ||
$result = call_user_func($this->withDefault); | ||
} elseif (is_array($this->withDefault)) { | ||
$result = $this->related->newInstance($this->withDefault); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would probably want to forceFill these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taylorotwell done.
e56b33a
to
99d7169
Compare
if (is_callable($this->withDefault)) { | ||
return call_user_func($this->withDefault); | ||
$result = call_user_func($this->withDefault); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why would the callback want to return
null
? That's a little weird. - Wouldn't it be better to pass a new linked-up model to the callback?
Maybe something like this:
protected function getDefaultFor(Model $model)
{
if (! $this->withDefault) {
return;
}
$instance = $this->related->newInstance()->setAttribute(
$this->getPlainForeignKey(), $model->getAttribute($this->localKey)
);
if (is_callable($this->withDefault)) {
return call_user_func($this->withDefault, $instance);
}
if (is_array($this->withDefault)) {
$instance->forceFill($this->withDefault);
}
return $instance;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I don't know, I just wanted to leave that possibility.
- I really like how you refactored the code 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So are you going to refactor the code to Joseph's suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taylorotwell done. If nothing is returned from the closure, the default instance will be returned automatically.
Thank you @JosephSilber! The code looks better now.
6149832
to
e98e75c
Compare
e98e75c
to
d7bfc91
Compare
There are 3 commits here:
First add test for the closure option.
Add option to pass an array of attributes for the default model, i.e.:
null
.